// pages/mypage/mypage.js
import {
  my_request,
  my_showToast_error,
  my_showToast_ok,
  gen_query_str
} from '../../utils/my_utils'

import base64 from '../../utils/base64'

const meetingSalt = getApp().data.meetingSalt

Page({

  /**
   * 页面的初始数据
   */
  data: {
    has_add_perm:wx.getStorageSync("has_add_perm"),
    select_status:1,
    createList:wx.getStorageSync("mypage-createList"),
    scriptList:wx.getStorageSync("mypage-scriptList"),
    attendList: []
  },

  load_script:function(){
    var that = this;
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/my-script-conference',
      method: "GET",
      success: function (res) {
        //console.log(res.data.data.conference);
        that.setData({
          scriptList: res.data.data.conference
        });
        wx.setStorageSync("mypage-scriptList",res.data.data.conference)
      },
      fail: function (res) {
        my_showToast_error("获取异常",2000)
      }
    })
  },
  nav_status:function(e){
    var status=e.currentTarget.dataset.status;
    if(status==2){
      this.load_my_create();
    }
    else if(status==3){
      this.load_script();
    }
    else if(status==1){
      this.load_my_attend();
    }
    this.setData({
      select_status:status
    })
  },
  nav:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  load_my_create:function(){
    var that = this;
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/my-create-conference',
      method: "GET",
      success: function (res) {
        //console.log(res.data.data.conference);
        that.setData({
          createList: res.data.data.conference
        });
        wx.setStorageSync("mypage-createList",res.data.data.conference)
      },
      fail: function (res) {
        my_showToast_error("获取异常",2000)
      }
    })
  },
  load_my_attend:function(){
    var that = this;
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/my-attend-conference',
      method: "GET",
      success: function (res) {
        that.setData({
          attendList: res.data.data.attend_list
        });
        wx.setStorageSync("mypage-attendList",res.data.data.attend_list)
      },
      fail: function (res) {
        my_showToast_error("获取异常",2000)
      }
    })
  },
  onShow: function () {
    //console.log("onshow")
    var from_send_to_check=wx.getStorageSync("from-send-to-check");
    //console.log(from_send_to_check)
    wx.removeStorageSync("from-send-to-check");
    var that=this;
    //获取授权信息
    //publisher-identities
    my_request({
      url:"https://sichuangyi.demopp.in/api-conference-alpha/have-add-perm",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function(res){
        that.setData({
          has_add_perm:true,
        })
        //that.load_script();
        wx.setStorageSync("has_add_perm",that.data.has_add_perm);
        var perm_list = [];
        var id_list=[]
        var data_list = res.data.data.list;
        for(var i=0;i<data_list.length;++i){
          var desc = data_list[i].contact_name+'\n'+data_list[i].contact_phone+"\n"+data_list[i].contact_org;
          perm_list.push(desc)
          id_list.push(data_list[i].id)
        }
        wx.setStorageSync("publisher-identities",perm_list);
        wx.setStorageSync("publisher-identities-ids",id_list);
      },
      fail:function(){
        that.setData({
          has_add_perm:false
        })
        wx.setStorageSync("has_add_perm",that.data.has_add_perm);
        that.load_my_attend();
      },
      complete: function() {
        //console.log(from_send_to_check)
        if(from_send_to_check){
          that.setData({
            select_status:2
          })
          that.load_my_create()
        }
      }
    })
    this.load_my_attend()
  },

  go_to_delete:function(e){
    var that=this;
    wx.showModal({
      title: '确认删除',
      content: '删除后将无法恢复，请慎重',
      success: function(res) {
        if (res.confirm) {
          var post_data={
            meeting:+e.currentTarget.dataset.conference_id,
            publisher:+e.currentTarget.dataset.creat_identity_id
          }
          my_request({
            url: 'https://sichuangyi.demopp.in/api-conference-alpha/remove',
            method: "POST",
            data: post_data,
            success:function(res){
              my_showToast_ok("删除成功",2000)
              that.load_script()
            },
            fail: function() {
              my_showToast_error("删除失败",2000)
            }
          })
        } else if (res.cancel) {
          return
        }
      }
    })
  },
  go_to_valid:function(){
    if(this.data.has_add_perm){
      wx.navigateTo({
        url:'/pages/conference/input_first'
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/validcode/validcode'
      })
    }
  },
  go_to_test:function(){
    wx.navigateTo({
      url:'/pages/conference/input_first'
    })
  },
  go_to_test_valid:function(){
    wx.navigateTo({
      url: '/pages/validcode/validcode'
    })
  },
  go_to_edit:function(e){
    var step = +e.currentTarget.dataset.step;
    var edit_id = +e.currentTarget.dataset.conference_id;
    var publisher = +e.currentTarget.dataset.creat_identity_id;
    var query_data={
      edit_id:edit_id,
      publisher:publisher,
    }
    if(step===0){
      wx.navigateTo({
        url:"/pages/conference/input_first"+gen_query_str(query_data)
      })
    }
    else if(step===1){
      wx.navigateTo({
        url:"/pages/conference/input_second"+gen_query_str(query_data)
      })
    }
    else if(step===2){
      wx.navigateTo({
        url:"/pages/conference/input_third"+gen_query_str(query_data)
      })
    }
  },
  go_to_detail: function(e) {
    let target = e.currentTarget
    let meeting = target.dataset.meeting
    if (!meeting) {
      return
    }
    wx.navigateTo({
      url: '/pages/ActivityDetail/index?id=' + meeting
    })
  },
  genQRcode({ currentTarget }) {

    let meeting = currentTarget.dataset.meeting
    
    if (!meeting) {
      return
    }

    let meeting_name = this.data.createList.reduce((acc, cur) => {
      if (cur.id == meeting) {
        acc = cur.meeting_name
      }
      return acc
    }, '企创宝会议')

    let base64ID = base64.encode(meetingSalt + meeting)

    wx.navigateTo({
      url: `/pages/SignUpQRcode/index?id=${base64ID}&name=${meeting_name}`
    })
  }
})